package logic

import (
	"context"
	"errors"
	"pro-train-hub/models/mysql"

	"pro-train-hub/trainrpc/internal/svc"
	"pro-train-hub/trainrpc/trainrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLessonsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLessonsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLessonsLogic {
	return &AddLessonsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddLessonsLogic) AddLessons(in *trainrpc.AddLessonsRequest) (*trainrpc.AddLessonsResponse, error) {
	// todo: add your logic here and delete this line
	var lesson mysql.Lesson
	mysql.DB.Debug().Where("title = ?", in.Title).Find(&lesson)
	if lesson.ID != 0 {
		return nil, errors.New("该课程已存在")
	}
	lesson = mysql.Lesson{
		Info:    in.Info,
		Area:    in.Area,
		Picture: "http://t2u3h1i1n.hn-bkt.clouddn.com/19a4c26f880511ebb6edd017c2d2eca2.jpg",
		UserId:  in.UserId,
		Title:   in.Title,
		Price:   in.Price,
	}
	mysql.DB.Debug().Create(&lesson)
	return &trainrpc.AddLessonsResponse{
		LessonId: int64(lesson.ID),
	}, nil
}
